/*******************************************************************************
 * FilePath    : /Software/Examples_Ble_MS1656/ble_app_peripheral_uart_simple/Src/BSP/TIM1.c
 * Version     : 1.0
 * Brief       : 
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-04-03 15:06:14
 * LastEditors : AndrewHu
 * LastEditTime: 2021-06-11 11:31:43
 * Description :  
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef Tim1_Handle = {0};
TIM_OC_InitTypeDef Tim1_OcInitHandle = {0};
u32 Tim1_CH2_Pulse = 0x00;
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/
/*******************************************************************************
 * Function    : Tim1_Init
 * Brief       : 灯初始化
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-04-03 16:01:38
 * Description : 
 ******************************************************************************/
void Tim1_Init(void)
{
    /* Compute the prescaler value to have TIMx counter clock equal to TIMx_FREQ(10000) Hz */
    // 预分频值;
    uint32_t PrescalerValue = (uint32_t)(HAL_RCC_GetPCLKFreq() / TIM1_FREQ) - 1;

    __HAL_RCC_TIM1_CLK_ENABLE();

    Tim1_Handle.Instance = TIM1;
    Tim1_Handle.Init.Period = TIM1_PERIOD;             // TIM1_ARR 周期
    Tim1_Handle.Init.Prescaler = PrescalerValue;       // 计数器的时钟频率(CK_CNT)等于fCK_PSC/(PSC[15:0]+1) 即计数器的时钟频率=TIMx_FREQ
    Tim1_Handle.Init.ClockDivision = 0;                // CKD 时钟分频因子(Clock division)
    Tim1_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; // 边沿对齐模式 计数器向上计数
    Tim1_Handle.Init.RepetitionCounter = 0;            // TIM1_RCR 重复计数器的值
    // 禁止自动重装载
    Tim1_Handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

    if (HAL_TIM_PWM_Init(&Tim1_Handle) != HAL_OK)
    {
        /* Initialization Error */
        Error_Handler();
    }
}
/*******************************************************************************
 * Function    : Tim1_CH2_Init
 * Brief       : 通道2输出初始化
 * param        {*}
 * return       {*}
 * Description : 
 * Date        : 2021-06-11 10:11:24
 ******************************************************************************/
void Tim1_CH2_Init(void)
{

    // Tim1_OcInitHandle.OCMode = TIM_OCMODE_PWM1;              // PWM模式1 TIM1_CCMR1
    Tim1_OcInitHandle.OCMode = TIM_OCMODE_PWM1;
    Tim1_OcInitHandle.OCPolarity = TIM_OCPOLARITY_LOW;       // 输出极性 OCx低电平有效
    Tim1_OcInitHandle.OCNPolarity = TIM_OCNPOLARITY_HIGH;    // OCxN低电平有效
    Tim1_OcInitHandle.OCFastMode = TIM_OCFAST_ENABLE;        // 输出比较1清’0’使能
    Tim1_OcInitHandle.OCIdleState = TIM_OCIDLESTATE_RESET;   // MOE=0时，如果实现了OC1N，则死区后OC1=0；
    Tim1_OcInitHandle.OCNIdleState = TIM_OCNIDLESTATE_RESET; // MOE=0时，死区后OC1N=0

    Tim1_CH2_Pulse = TIM1_CH2_PULSEWIDTH;
    Tim1_OcInitHandle.Pulse = Tim1_CH2_Pulse;
    if ((HAL_TIM_PWM_ConfigChannel(&Tim1_Handle, &Tim1_OcInitHandle, TIM_CHANNEL_2)) != HAL_OK)
    {
        /* Initialization Error */
        Error_Handler();
    }

    TIM_CCxChannelCmd(TIM1, TIM_CHANNEL_2, TIM_CCxN_ENABLE);

    /* Start channel 2 */
    if (HAL_TIM_PWM_Start(&Tim1_Handle, TIM_CHANNEL_2) != HAL_OK)
    {
        /* Starting Error */
        Error_Handler();
    }
}
/*******************************************************************************
 * Function    : Tim1_CH2_SetPulse
 * Brief       : 更新PWM占空比
 * param        {u32} value
 * return       {*}
 * Description : 
 * Date        : 2021-06-11 10:17:06
 ******************************************************************************/
void Tim1_CH2_SetPulse(u32 value)
{
    Tim1_Handle.Instance->CCR2 = value;
    Tim1_CH2_Pulse = value;
}
/*******************************************************************************
 * Function    : Tim1_CH2_GetPulse
 * Brief       : 获取占空比
 * param        {*}
 * return       {*}
 * Description : 
 * Date        : 2021-06-11 10:41:20
 ******************************************************************************/
u32 Tim1_CH2_GetPulse(void)
{
    return Tim1_CH2_Pulse;
}
